const path = require('path')
const Alipay = require('alipay-node-sdk')

// 实例化alipay对象
var alipay = new Alipay({
    appId: '2016082600315647',
    // notifyUrl: 'http://www.xxx.com/callback/alipay',
    rsaPrivate: path.resolve(__dirname,'./rsa2_private_key.pem'),
    rsaPublic: path.resolve(__dirname,'./rsa2_public_key.pem'),
    sandbox: true,
    signType: 'RSA2'
});

/**
 * 返回支付的url
 */
exports.getPayURL = (order) => {
    var params = alipay.pagePay({
        subject: `PYG-${order.order_number}`,
        // 商品描述，把多个商品名称用换行分隔开
        body: order.products.map(item=>item.name).join("\n"),
        outTradeId: order.order_number,
        timeout: '10m',
        amount: order.total_price,
        return_url:'http://localhost:3000/pay/success',
        // 商品主类型 0 虚拟商品 1 实物商品
        goodsType: '1',
        qrPayMode: 2
    })

    return `https://openapi.alipaydev.com/gateway.do?${params}`
}